package com.hsxc.cdz.controller.consoleController;

import javax.servlet.http.HttpSession;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsxc.cdz.base.bean.ConsoleUser;
import com.hsxc.cdz.mgmt.mobileMessage.MobileMessage;
import com.hsxc.cdz.mgmt.user.UserManager;

import net.sf.json.JSONObject;

/**
 * @author wpfei 手机验证码验证和用户密码重置
 */
@Controller
@RequestMapping("/api")
public class mobileMessageController {
	Logger logger = LoggerFactory.getLogger(mobileMessageController.class);
	@Autowired
    UserManager userManager;

	@RequestMapping(value = "/mobileMessage", method = RequestMethod.GET)
	@ResponseBody
	// 根据手机号码获取验证码
	public JSONObject getMobileMessage(@RequestParam("mobile") String mobile, HttpSession httpSession) {
		JSONObject result = new JSONObject();
		result.put("success", true);
		logger.debug("MOBILE NUMBER:[" + mobile + "]");
		ConsoleUser consoleUser = userManager.loadConUserByMobile(mobile);
		logger.debug("得到的对象为" + consoleUser);
		String code = "";
		if (consoleUser != null) {
			MobileMessage.sendMsg(mobile);
			logger.info("发送出去的验证码是code：" + code);
			result.put("errcode", 0);
			result.put("message", "获取手机号码验证码成功");
			logger.info("获取手机号码验证码成功");
		} else {
			result.put("errcode", 1);
			result.put("message", "该手机号码未注册");
			logger.info("该手机号码未注册");
		}
		return result;
	}

	@RequestMapping(value = "/checkMobileMessage", method = RequestMethod.POST)
	@ResponseBody
	// 校验验证码
	public JSONObject checkMobileMessage(@RequestBody String jsonObj, HttpSession httpSession) {
		JSONObject result = new JSONObject();
		org.json.JSONObject jsonObject = null;
		String mobile = "";
		String vireCode = "";
		try {
			jsonObject = new org.json.JSONObject(jsonObj);
			result.put("success", true);
			if (jsonObject.has("code")) {
				vireCode = jsonObject.getString("code");
			}
			if (jsonObject.has("mobile"))
				mobile = jsonObject.getString("mobile");
			logger.debug("vireCode为" + vireCode);
			logger.debug("code" + vireCode);
			boolean check = MobileMessage.checkMsg(mobile, vireCode);
			if (check) {
				result.put("added", true);
				result.put("errcode", 0);
				result.put("message", null);
				logger.info("message" + "手机验证码验证成功");
			} else {
				result.put("added", false);
				result.put("errcode", 1);
				result.put("message", "验证码错误");
				logger.info("message" + "验证码错误");
			}

		} catch (JSONException e1) {
			result.put("success", false);
			e1.printStackTrace();
			logger.info("message" + e1.getMessage());
		}
		return result;
	}

	// =====================================================================================================
	@RequestMapping(value = "/updateSecret", method = RequestMethod.PUT)
	@ResponseBody
	// 修改用户密码
	public JSONObject updateSecret(@RequestBody String obj, HttpSession httpSession) {
		JSONObject result = new JSONObject();
		ConsoleUser consoleUser = new ConsoleUser();
		result.put("success", true);
		int ii = -1;
		org.json.JSONObject jsonObject;
		try {
			jsonObject = new org.json.JSONObject(obj);
			String mobile = (String) httpSession.getAttribute("mobile");
			httpSession.setAttribute("mobile", null);
			logger.debug("===========================" + "手机号码为" + mobile + "=========================");
			String password = jsonObject.getString("password");
			consoleUser.setPassword(password);
			consoleUser.setMobile(mobile);
			ii = userManager.updateConUser(consoleUser);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		if (ii >= 0) {
			result.put("added", true);
			result.put("errcode", 0);
			result.put("message", "密码更新成功");
			logger.info("message" + "密码更新成功");

		} else {
			result.put("added", false);
			result.put("errcode", 1);
			result.put("message", "密码更新失败");
			logger.info("message" + "密码更新失败");
		}

		return result;

	}
}
